package com.sinosoft.hydra.um.helper;

import org.apache.commons.lang.StringUtils;

import java.net.URL;

/**
 * Created with IntelliJ IDEA.
 * User: New
 * Date: 13-2-2
 * Time: 上午10:35
 * To change this template use File | Settings | File Templates.
 */
public class LocationHelper {

    private static LocationHelper instance = new LocationHelper();
    private static String SPR = "WEB-INF";

    public static LocationHelper getInstance() {
        return instance;
    }

    private LocationHelper() {

    }

    public String getRootPath() {
        URL url = this.getClass().getClassLoader().getResource("");
        String path = url.getPath();
        int start = path.indexOf("/") == 0 ? 1 : 0;
        return StringUtils.substring(path, start, StringUtils.indexOf(path, SPR));
    }

    public String getClassPath() {
        URL url = this.getClass().getClassLoader().getResource("");
        String path = url.getPath();
        int start = path.indexOf("/") == 0 ? 1 : 0;
        return StringUtils.substring(path, start, StringUtils.lastIndexOf(path, "/"));
    }

    public static String getRootUrl(Class<?> t) {
        URL _rootPath = t.getResource("/");
        int start = _rootPath.toString().indexOf("/");
        String _path = _rootPath.toString().substring(start + 1);
        return _path;
    }
}
